package 第四章;

public class Yinxiangdian {
	Cd d[] = new Cd[3];

	public void init() {
		for (int i = 0; i < d.length; i++) {// 不加大同不能自动加this？
			if (d[i] == null) {
				d[i] = new Cd();
			}
		}
		d[0].name = "凡人";
		d[1].name = "修仙";
		d[2].name = "传";
		d[0].state = false;
		d[1].state = false;
		d[2].state = false;
		// d[0].date = 0;
		// d[1].date = 0;
		// d[2].date = 0;
		d[0].count = 0;
		d[1].count = 0;
		d[2].count = 0;
		System.out.println("初始化成功...");
	}

	public void add(Cd s) {
		int c = 0;
		for (int i = 0; i < d.length; i++) {
			if (d[i] == null) {
				d[i] = s;
				// s.date = 0;
				s.count = 0;
				c = 1;
				System.out.println("增加成功！");
			}
		}
		if (c == 0)
			System.out.println("增加失败，位置已满！");
	}

	public void showinfo() {
		for (int i = 0; i < d.length; i++) {
			if (d[i] != null)
				System.out.println("书名：" + d[i].name + " " + "借出状态："
						+ d[i].state + " " + /* "借出天数：" + d[i].date + */" "
						+ "已被借出次数：" + d[i].count);
		}
	}

	public void delete(String name) {
		int a = 0;
		for (int i = 0; i < d.length; i++) {
			if (d[i] != null && d[i].state == false && name.equals(d[i].name)) {
				d[i] = null;
				a = 1;
				System.out.println("删除成功");
			}
		}
		if (a == 0)
			System.out.println("位置已满或已借出无法删除");
	}

	public void jiechu(String name1) {
		int b = 0;
		for (int i = 0; i < d.length; i++) {
			if (d[i] != null && d[i].name.equals(name1) && d[i].state == false) {
				d[i].state = true;
				d[i].count++;
				b = 1;
				System.out.println("借书成功!");
				// return days*5;
			}
		}
		if (b == 0)
			System.out.println("借书失败!");
		// return -1;
	}

	public int guihuan(String name, int days) {
		// int ee = 0;
		for (int i = 0; i < d.length; i++) {
			if (d[i] != null && name.equals(d[i].name) && d[i].state == true) {
				d[i].state = false;
				System.out.println("归还成功！");
				// ee = 1;
				return days * 5;
			}
		}
		// if (ee == 0) {
		// System.out.println("归还失败！");
		return -1;
		// }
	}

}
